<?php

function abreLinha()
{
    ?><div class="row"><?php
}

function fechaLinha()
{
    ?></div><?php
}

function abreFieldset($legend)
{
    ?><fieldset class='row'>
      <legend><?php echo $legend; ?></legend>
      <div class="container-fluid"><?php
}

function fechaFieldset()
{
    ?></div></fieldset><?php
}

//inicializando as variaveis para controle do formulário
$cdForm = null;
$stSubForm = "";
$clienteModel = ZC_Model::factory(ZC_Model::MODEL_SITE_CLIENTE);
$enderecoModel = new Model_Endereco();

$camposEnderecoAll = $enderecoModel->getAllCamposFixosName();
$camposAll = $clienteModel->getAllCamposFixosName();
$camposName = $clienteModel->getCamposFixosCliente();
$camposFName = $clienteModel->getCamposFixosClienteF();
$camposJName = $clienteModel->getCamposFixosClienteJ();
$i = 0;

?>
<form id="frmCadastro" name="frmCadastro" enctype="application/x-www-form-urlencoded" method="post" action="" class='col-xs-12'>
    <?php
    foreach ($this->campos as $row) {

        $erro = false;
        $value = "";

        if (($stSubForm != $row['stSubForm']) && ($cdForm != null)) {
            fechaLinha();
            fechaFieldset();
        }

        if ($stSubForm != $row['stSubForm']) {
            $iSubForm = 0;
            $stSubForm = $row['stSubForm'];
            
            abreFieldset($stSubForm);
            abreLinha();
            }

            // Campo personalizado
            $field = '';
            if ($row['cdTipo'] != null) {
                $field = 'cp' . $row['cdCampo'];
                $name = 'dadospersonalizados[' . $field . ']';
                if (isset($this->erros['dadospersonalizados'])) {
                    if (isset($this->erros['dadospersonalizados'][$field])) {
                        $erro = $this->erros['dadospersonalizados'][$field];
                    }
                }

                if (isset($this->frmValues['dadospersonalizados'])) {
                    if (isset($this->frmValues['dadospersonalizados'][$field])) {
                        $value = $this->frmValues['dadospersonalizados'][$field];
                    }
                }

                $name = 'dadospersonalizados[cp' . $row['cdCampo'] . ']';
                switch ($row['cdTipo']) {
                    case 5:
                        $tipo = 'combobox';
                        $opcoes = json_decode($row['stConfigCp'], 1);
                        $values = $opcoes['opcoes'];
                        break;
                    case 17:
                        $tipo = 'checkbox';
                        break;
                    case 16:
                        $tipo = 'date';
                        break;
                    default:
                        $tipo = 'text';
                        break;
                }
            }
            // Campo fixo
            else {
                if ($row['cdTipoSubForm'] == 1) {
                if (in_array($row['cdCampo'], $camposName)) {
                    $name = $camposAll[$row['cdCampo']];
                    if (isset($this->erros[$name])) {
                        $erro = $this->erros[$name];
                    }
                    if (isset($this->frmValues[$name])) {
                        $value = $this->frmValues[$name];
                    }
                } else if (in_array($row['cdCampo'], $camposFName)) {
                    $field = $camposAll[$row['cdCampo']];
                    $name = 'pessoaF[' . $field . ']';
                    if (isset($this->erros['pessoaF'])) {
                        if (isset($this->erros['pessoaF'][$field])) {
                            $erro = $this->erros['pessoaF'][$field];
                        }
                    }
                    if (isset($this->frmValues['pessoaF'])) {
                        if (isset($this->frmValues['pessoaF'][$field])) {
                            $value = $this->frmValues['pessoaF'][$field];
                        }
                    }
                } else if (in_array($row['cdCampo'], $camposJName)) {
                    $field = $camposAll[$row['cdCampo']];
                    $name = 'pessoaJ[' . $camposAll[$row['cdCampo']] . ']';
                    if (isset($this->erros['pessoaJ'])) {
                        if (isset($this->erros['pessoaJ'][$field])) {
                            $erro = $this->erros['pessoaJ'][$field];
                        }
                    }

                    if (isset($this->frmValues['pessoaJ'])) {
                        if (isset($this->frmValues['pessoaJ'][$field])) {
                            $value = $this->frmValues['pessoaJ'][$field];
                        }
                    }
                } 
            } else {
                    if (isset($camposEnderecoAll[$row['cdCampo']])) {
                            $name = $camposEnderecoAll[$row['cdCampo']];
                            if (isset($this->erros[$name])) {
                                $erro = $this->erros[$name];
                            }
                            if (isset($this->frmValues[$name])) {
                                $value = $this->frmValues[$name];
                            }
                    }
                }

                if ($row['cdTipoSubForm'] == 1) {
                    switch ($row['cdCampo']) {
                            case 7:
                            case 8:
                                $tipo = 'telefone';
                                break;
                            case 9:
                            case 10:
                                $tipo = 'email';
                                break;
                            case 11:
                            case 12:
                                $tipo = 'password';
                                break;
                            case 17:
                                $tipo = 'checkbox';
                                break;
                            default:
                                $tipo = 'text';
                                break;
                        }
                    } else {
                        switch ($row['cdCampo']) {
                            case 4:
                                $tipo = 'cidade';
                                break;
                            case 10:
                                $tipo = 'telefone';
                                break;
                            default:
                                $tipo = 'text';
                                break;
                        }
                    }
               
            }

            if (($iSubForm > 0) && (($iSubForm % 2) == 0)) {
                fechaLinha();
                abreLinha();
            }
            
            $formGroupClass = 'form-group col-xs-12 col-sm-6 container-fluid';
            if ($erro) {
                $formGroupClass .= ' has-error';
            }
            ?>
          <div class="<?php echo $formGroupClass; ?>">
              <?php
              switch ($tipo) {
                  case 'telefone':
                      ?>
              <div class="row">
                    <label for="<?php echo $i; ?>" class="col-xs-12"><?php echo $row['stCampo']; ?></label>
                    <span class="col-xs-4">
                      <input class="form-control <?php echo $field; ?>" type="text" id="<?php echo $i; ?>" name="<?php echo $name; ?>[ddd]" value="<?php echo (isset($value['ddd'])) ? $value['ddd'] : ''; ?>" maxlength="2">
                    </span>
                    <span class="col-xs-8">
                      <input class="form-control <?php echo $field; ?>" type="text" id="<?php echo $i; ?>" name="<?php echo $name; ?>[number]" value="<?php echo (isset($value['number'])) ? $value['number'] : ''; ?>">
                    </span>
              </div>
                    <?php
                    break;
                case 'checkbox':
                    ?>
                    <div class="checkbox">
                      <label for="<?php echo $i; ?>">
                        <input type="<?php echo $tipo; ?>" id="<?php echo $i; ?>" name="<?php echo $name; ?>" value="1">
                        <?php echo $row['stCampo']; ?>
                      </label>
                    </div>
                    <?php
                    break;
                case 'cidade':
                    ?>
              <div class="row">
                    <span class="col-xs-4">
                        <label for="<?php echo $i; ?>" class="control-label">Uf</label>
                        <select class="form-control" id="frmEndereco_cdCidadeUf" name="cdUf">
                            <option value="">Selecione...</option>
                            <?php
                            $ufs = ZC_Model::factory(ZC_Model::TABLE_UF)->fetchAll()->toArrayAssoc('cdUf', 'stUf');
                            foreach ($ufs as $cdUf => $stUf) {
                                ?>
                                <option value="<?php echo $cdUf; ?>"><?php echo $stUf; ?></option>
                            <?php } ?>
                        </select>
                    </span>
                    <span class="col-xs-8">
                        <label for="<?php echo $i; ?>" class="control-label">Cidade</label>
                        <select class="form-control" id="frmEndereco_cdCidade" name="<?php echo $name; ?>">
                            <option>Selecione o uf</option>
                        </select>
                    </span>
          </div>
                    <?php
                    break;    
                case 'combobox':
                ?>
              <div class="row">
                    <span class="col-xs-12">
                        <label for="<?php echo $i; ?>" class="control-label"><?php echo $row['stCampo']; ?></label>
                        <select class="form-control" id="<?php echo $i; ?>" name="<?php echo $name; ?>">
                            <option value="">Selecione...</option>
                            <?php foreach ($values as $k => $value) { ?>
                                <option value="<?php echo $k; ?>"><?php echo $value; ?></option>
                            <?php } ?>
                        </select>
                    </span>
              </div>
                   <?php
                    break;    
                default :
                    ?>
                    <label for="<?php echo $i; ?>"><?php echo $row['stCampo']; ?></label>
                    <input class=" form-control <?php echo $field; ?>" type="<?php echo $tipo; ?>" id="<?php echo $i; ?>" name="<?php echo $name; ?>" value="<?php echo $value; ?>">
                    <?php
                    break;
            }

            if ($erro) {
                foreach ($erro as $msg) {
                    ?>
                    <span id="helpBlock" class="help-block"><?php echo $msg; ?></span>
                    <?php
                }
            }
            ?>

          </div>
          <?php
          //if (($iSubForm > 0) && (($iSubForm % 2) == 0)) {
              //fechaLinha();
          //}

          $cdForm = $row['cdForm'];
          $i++;
          $iSubForm++;
      }

      fechaLinha();

      ?>

    <input type="hidden" name="cadastrar" value="1">
    <input type="hidden" name="cdForm" value="<?php echo $cdForm; ?>">
    <button type="submit" name="btCadastro">AVANÇAR</button>
  </fieldset>

</form>
